package com.lmqb.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.lmqb.bean.GrabCarrierBills;
import com.lmqb.bean.GrabCarrierCalls;
import com.lmqb.bean.GrabCarrierFamilies;
import com.lmqb.bean.GrabCarrierNets;
import com.lmqb.bean.GrabCarrierSmses;
import com.lmqb.bean.GrabCarrierUserinfo;
import com.lmqb.bean.GrabFaceRecognition;
import com.lmqb.bean.GrabMobileContacts;
import com.lmqb.bean.GrabQqContacts;
import com.lmqb.bean.GrabQqRecentcontacts;
import com.lmqb.bean.GrabQqUserinfo;
import com.lmqb.bean.GrabTaobaoAlipaywealth;
import com.lmqb.bean.GrabTaobaoDeliveraddress;
import com.lmqb.bean.GrabTaobaoRecentdeliveraddress;
import com.lmqb.bean.GrabTaobaoTradedetails;
import com.lmqb.bean.GrabTaobaoUserinfo;
import com.lmqb.bean.GrabWeixin;
import com.lmqb.bean.GrabXuexin;
import com.lmqb.bean.GrabZhima;
import com.lmqb.bean.JCheckArtificial;
import com.lmqb.bean.JCollection;
import com.lmqb.bean.JRefuseReason;
import com.lmqb.bean.LmAccount;
import com.lmqb.bean.LmAccountLog;
import com.lmqb.bean.LmAgentApply;
import com.lmqb.bean.LmCredit;
import com.lmqb.bean.LmLoan;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.LmSchedule;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.LmUserBank;
import com.lmqb.bean.LmUserChange;
import com.lmqb.bean.LmUserCheckLog;
import com.lmqb.bean.LmUserEmergency;
import com.lmqb.bean.LmUserInfo;
import com.lmqb.bean.LmUserJob;
import com.lmqb.bean.LmUserLoginLog;
import com.lmqb.bean.LmUserToken;
import com.lmqb.bean.LmWithdraw;
import com.lmqb.bean.LogSms;
import com.lmqb.bean.LogWeixin;
import com.lmqb.bean.SysArea;
import com.lmqb.bean.SysRolemenu;
import com.lmqb.bean.SysUser;
import com.lmqb.constant.Status;
import com.lmqb.dao.GrabCarrierBillsMapper;
import com.lmqb.dao.GrabCarrierCallsMapper;
import com.lmqb.dao.GrabCarrierFamiliesMapper;
import com.lmqb.dao.GrabCarrierNetsMapper;
import com.lmqb.dao.GrabCarrierSmsesMapper;
import com.lmqb.dao.GrabCarrierUserinfoMapper;
import com.lmqb.dao.GrabFaceRecognitionMapper;
import com.lmqb.dao.GrabMobileContactsMapper;
import com.lmqb.dao.GrabQqContactsMapper;
import com.lmqb.dao.GrabQqRecentcontactsMapper;
import com.lmqb.dao.GrabQqUserinfoMapper;
import com.lmqb.dao.GrabTaobaoAlipaywealthMapper;
import com.lmqb.dao.GrabTaobaoDeliveraddressMapper;
import com.lmqb.dao.GrabTaobaoRecentdeliveraddressMapper;
import com.lmqb.dao.GrabTaobaoTradedetailsMapper;
import com.lmqb.dao.GrabTaobaoUserinfoMapper;
import com.lmqb.dao.GrabWeixinMapper;
import com.lmqb.dao.GrabXuexinMapper;
import com.lmqb.dao.GrabZhimaMapper;
import com.lmqb.dao.JCheckArtificialMapper;
import com.lmqb.dao.JCollectionMapper;
import com.lmqb.dao.JRefuseReasonMapper;
import com.lmqb.dao.LmAccountLogMapper;
import com.lmqb.dao.LmAccountMapper;
import com.lmqb.dao.LmAgentApplyMapper;
import com.lmqb.dao.LmCreditMapper;
import com.lmqb.dao.LmLoanMapper;
import com.lmqb.dao.LmRepayMapper;
import com.lmqb.dao.LmScheduleMapper;
import com.lmqb.dao.LmUserBankMapper;
import com.lmqb.dao.LmUserChangeMapper;
import com.lmqb.dao.LmUserCheckLogMapper;
import com.lmqb.dao.LmUserEmergencyMapper;
import com.lmqb.dao.LmUserInfoMapper;
import com.lmqb.dao.LmUserJobMapper;
import com.lmqb.dao.LmUserLoginLogMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.LmUserTokenMapper;
import com.lmqb.dao.LmWithdrawMapper;
import com.lmqb.dao.LogSmsMapper;
import com.lmqb.dao.LogWeixinMapper;
import com.lmqb.dao.SysAreaMapper;
import com.lmqb.dao.SysMenuMapper;
import com.lmqb.dao.SysRolemenuMapper;
import com.lmqb.dao.SysUserMapper;
import com.lmqb.entry.DaiLi;
import com.lmqb.entry.TuiGuang;
import com.lmqb.entry.UBaseInfo;
import com.lmqb.entry.ULoan;
import com.lmqb.entry.UUserLoan;
import com.lmqb.page.AgentApplyPager;
import com.lmqb.page.LmDaiLiPager;
import com.lmqb.page.LmUserPager;
import com.lmqb.page.PageTool;
import com.lmqb.page.UserBankPager;
import com.lmqb.page.UserChangePager;
import com.lmqb.page.UserEmergencyPager;
import com.lmqb.page.UserJobPager;
import com.lmqb.page.UserLoginPager;
import com.lmqb.page.UserTokenPager;
import com.lmqb.service.UserService;
import com.lmqb.util.DesUtils;
import com.lmqb.util.PageModel;
import com.lmqb.util.StrUtil;

@Service("userService")
public class LmUserServiceImpl implements UserService{

	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
	private LmUserTokenMapper userTokenDao;
	
	@Autowired
	private LmUserLoginLogMapper userLoginDao;
	
	@Autowired
	private LmAgentApplyMapper agentApplyDao;
	
	@Autowired
	private LmUserBankMapper userBankDao;
	
	@Autowired
	private LmUserJobMapper userJobDao;
	
	@Autowired
	private LmUserEmergencyMapper userEmergencyDao;
	
	@Autowired
	private LmUserChangeMapper userChangeDao;
	
	@Autowired
	private LmAccountMapper accountDao;
	
	@Autowired
	private LmWithdrawMapper withdrawDao;
	
	@Autowired
	private LmUserInfoMapper lmUserInfoDao;
	
	@Autowired
	private SysAreaMapper areaDao;
	
	@Autowired
	private JRefuseReasonMapper refuseDao;
	
	@Autowired
	private SysUserMapper sysUserDao;
	
	@Autowired
	private SysRolemenuMapper sysRoleDao;
	
	@Autowired
	private SysMenuMapper sysMenuDao;
	
	@Autowired
	private JCheckArtificialMapper checkDao;
	
	@Autowired
	private LmScheduleMapper scheduleDao;
	
	@Autowired
	private LmCreditMapper creditDao;
	
	@Autowired
	private GrabXuexinMapper xuexinDao;
	
	@Autowired
	private LmUserJobMapper workInfoDao;
	
	@Autowired
	private GrabZhimaMapper zhimaDao;
	
	@Autowired
	private GrabCarrierUserinfoMapper carrierDao;
	
	@Autowired
	private GrabCarrierFamiliesMapper carrierFamiliesDao;
	
	@Autowired
	private GrabCarrierBillsMapper carrierBillsDao;
	
	@Autowired
	private GrabCarrierCallsMapper carrierCallsDao;
	
	@Autowired
	private GrabCarrierSmsesMapper carrierSmsesDao;
	
	@Autowired
	private GrabCarrierNetsMapper carrierNetsDao;
	
	@Autowired
	private GrabQqUserinfoMapper qqUserinfoDao;
	
	@Autowired
	private GrabQqContactsMapper qqContactsDao;
	
	@Autowired
	private GrabQqRecentcontactsMapper recentDao;
	
	@Autowired
	private GrabTaobaoUserinfoMapper taobaoBaseDao;
	
	@Autowired
	private GrabTaobaoAlipaywealthMapper alipayweathDao;
	
	@Autowired
	private GrabTaobaoDeliveraddressMapper taobaoAddressDao;
	
	@Autowired
	private GrabTaobaoRecentdeliveraddressMapper recentAddressDao;
	
	@Autowired
	private GrabWeixinMapper grabWeixinDao;
	
	@Autowired
	private LmRepayMapper repayDao;
	
	@Autowired
	private LmLoanMapper loanDao;
	
	@Autowired
	private JCollectionMapper collecionDao;
	
	@Autowired
	private GrabFaceRecognitionMapper faceDao;
	
	@Autowired
	private LogSmsMapper logSmsDao;
	
	@Autowired
	private LogWeixinMapper logWeixinDao;
	
	@Autowired
	private LmUserCheckLogMapper checkLogDao;
	
	@Autowired
	private LmAccountLogMapper accountLogDao;
	//xiugai 6.30
	@Autowired
	private GrabTaobaoTradedetailsMapper TradedetailDao;
	
	@Autowired
	private GrabMobileContactsMapper grabMobileContactsMapper;

	private String qqContactsF;
	
	/**
	 * @show 登录令牌信息
	 * 
	 * @param UserTokenPager
	 * 
	 * @return UserTokenPager
	 * 
	 * */
	public UserTokenPager selectUserTokenPager(UserTokenPager pagerModel) {
		List<LmUserToken> list=userTokenDao.selectAll(pagerModel);

		List<LmUser> userList=lmUserDao.selectAllUser();
		
		for(int i=0;i<list.size();i++){
			list.get(i).setExpireTimeD(StrUtil.intToTime(list.get(i).getExpireTime()));
			list.get(i).setLastActionTimeD(StrUtil.intToTime(list.get(i).getLastActionTime()));
			list.get(i).setLoginTimeD(StrUtil.intToTime(list.get(i).getLoginTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = userTokenDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show 借款人登录日志
	 * 
	 * @param UserLoginPager
	 * 
	 * @return UserLoginPager
	 * 
	 * */
	public UserLoginPager selectUserLoginPager(UserLoginPager pagerModel) {
		
		List<LmUserLoginLog> list=userLoginDao.selectAll(pagerModel);
		System.out.println(list.size()+"-->"+list);
//		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setLoginTimeD(StrUtil.intToTime(list.get(i).getLoginTime()));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setUserName(userList.get(j).getLoanMobile());
//					break;
//				}
//			}
		}
		pagerModel.setList(list);
		int count = userLoginDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 查询用户列表 修改 7.5
	 * 
	 * @param LmUserPager
	 * 
	 * @return LmUserPager
	 * 
	 * */	
	public LmUserPager selectUserPager(LmUserPager pagerModel) {
		List<LmUser> list=lmUserDao.selectAll(pagerModel);
		
		
		
		
		
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setLastLoginTimeD(StrUtil.intToTime(list.get(i).getLastLoginTime()));
			if(list.get(i).getAuthStep()==null){
				list.get(i).setAuthStep((byte) -1);
			}
			
		}
		pagerModel.setList(list);
		
		int count = lmUserDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show 查询复借用户列表 修改 7.5 借款次数大于1
	 * 
	 * @param LmUserPager
	 * 
	 * @return LmUserPager
	 * 
	 * */		
  public  LmUserPager selectUserPagerAg(LmUserPager pagerModel) {
			List<LmUser> list=lmUserDao.selectAllLoanAgian(pagerModel);
			for(int i=0;i<list.size();i++){
				list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
				list.get(i).setLastLoginTimeD(StrUtil.intToTime(list.get(i).getLastLoginTime()));
			}
			pagerModel.setList(list);
			int count = lmUserDao.selectCountLoanAgian(pagerModel);
			pagerModel.setTotalRow(count);
			int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
			pagerModel.setTotalPage(totalPage);
			return pagerModel;
		}

	/**
	 * @show  代理商申请
	 * 
	 * @param AgentApplyPager
	 * 
	 * @return AgentApplyPager
	 *
	 * */
	public AgentApplyPager selectAgentApplyPager(AgentApplyPager pagerModel) {
		List<LmAgentApply> list=agentApplyDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setLastModifyTimeD(StrUtil.intToTime(list.get(i).getLastModifyTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setXingming(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = agentApplyDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 *@show 银行卡信息
	 * 
	 *@param UserBankPager
	 * 
	 *@return UserBankPager
	 * */
	public UserBankPager selectUserBankPager(UserBankPager pagerModel) {
		List<LmUserBank> list=userBankDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setDeleteTimeD(StrUtil.intToTime(list.get(i).getDeleteTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = userBankDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 *@show 工作信息
	 * 
	 *@param UserJobPager
	 *
	 *@return UserJobPager
	 * 
	 * */
	public UserJobPager selectUserJobPager(UserJobPager pagerModel) {
		List<LmUserJob> list=userJobDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setLastModifyTimeD(StrUtil.intToTime(list.get(i).getLastModifyTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = userJobDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 *@show 紧急联系人信息
	 * 
	 *@param  UserEmergencyPager
	 * 
	 *@return UserEmergencyPager
	 * */
	public UserEmergencyPager selectUserEmergencyPager(
			UserEmergencyPager pagerModel) {
		List<LmUserEmergency> list=userEmergencyDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = userEmergencyDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 *@show 信息变更
	 *
	 *@param UserChangePager
	 * 
	 *@return UserChangePager
	 * 
	 * */
	public UserChangePager selectUserChangePager(UserChangePager pagerModel) {
		List<LmUserChange> list=userChangeDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = userChangeDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

  /**
	*@show 代理列表 
    * 
	*@param LmDaiLiPager
	*
	*@return LmDaiLiPager
	* 
	* */
	public LmDaiLiPager selectDailiPager(LmDaiLiPager pagerModel) {
		LmUserPager userPager=new LmUserPager();
		userPager.setKey(pagerModel.getKey());
		userPager.setTime1Int(pagerModel.getTime1Int());
		userPager.setTime2Int(pagerModel.getTime2Int());
		userPager.setPage(pagerModel.getPage());
		userPager.setOffset(pagerModel.getOffset());
	    userPager.setAgentStatus((byte)2);
	    System.out.println(pagerModel.getOffset()+"---》"+pagerModel.getPage());
	    System.out.println(userPager.getOffset()+"-->"+userPager.getPage());
		List<LmUser> list=lmUserDao.selectAll(userPager);
		List<DaiLi> dailis=new ArrayList<DaiLi>();
		List<LmUserInfo> userInfos=lmUserInfoDao.selectAll();
		List<LmAgentApply> agentApplys=agentApplyDao.selectAllUser();
		List<LmAccount> accounts=accountDao.selectAllUser();
		List<SysArea> areas=areaDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			DaiLi daili=new DaiLi();
			daili.setUserId(list.get(i).getUserId());
			daili.setNickName(list.get(i).getNickName());
			daili.setMobile(list.get(i).getLoginMobile());
			for(int j=0;j<userInfos.size();j++){
				if(userInfos.get(j).getUserId().equals(list.get(i).getUserId())){
					daili.setAddress(userInfos.get(j).getLivingAddress());
					daili.setRealName(userInfos.get(j).getUserName());
					for(int m=0;m<areas.size();m++){
						if(areas.get(m).getAreaNo().equals(userInfos.get(j).getLivingAreaNo())){
							daili.setSheng(areas.get(m).getAreaFullName());
						}
					}
				}
			}
			for(int n=0;n<agentApplys.size();n++){
				if(agentApplys.get(n).getUserId().equals(list.get(i).getUserId())){
					daili.setYoushi(agentApplys.get(n).getApplyRemark());
					daili.setTime(StrUtil.intToTime(agentApplys.get(n).getLastModifyTime()));
					daili.setAgentStatus(agentApplys.get(n).getAgentStatus());
					
				}
			}
			for(int k=0;k<accounts.size();k++){
				if(accounts.get(k).getUserId().equals(list.get(i).getUserId())){
					daili.setErjiNum(accounts.get(k).getDirectUsers());
					daili.setYijiNum(accounts.get(k).getIndirectUsers());
				}
			}
			dailis.add(daili);
		}
		pagerModel.setList(dailis);
		int count = lmUserDao.selectCount(userPager);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	/**
	 * @show 代理审核
	 * 
	 * @param LmDaiLiPager
	 * 
	 * @return LmDaiLiPager
	 * 
	 * */
	public LmDaiLiPager selectAgentCheckPager(LmDaiLiPager pagerModel) {
		LmUserPager userPager=new LmUserPager();
		userPager.setKey(pagerModel.getKey());
		userPager.setTime1Int(pagerModel.getTime1Int());
		userPager.setTime2Int(pagerModel.getTime2Int());
		userPager.setPage(pagerModel.getPage());
		userPager.setOffset(pagerModel.getOffset());
		userPager.setAgentStatus((byte)1);
		List<LmUser> list=lmUserDao.selectAll(userPager);
		List<DaiLi> dailis=new ArrayList<DaiLi>();
		List<LmUserInfo> userInfos=lmUserInfoDao.selectAll();
		List<LmAgentApply> agentApplys=agentApplyDao.selectAllUser();
		List<LmAccount> accounts=accountDao.selectAllUser();
		List<SysArea> areas=areaDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			DaiLi daili=new DaiLi();
			daili.setUserId(list.get(i).getUserId());
			daili.setNickName(list.get(i).getNickName());
			daili.setMobile(list.get(i).getLoginMobile());
			for(int j=0;j<userInfos.size();j++){
				if(userInfos.get(j).getUserId().equals(list.get(i).getUserId())){
					daili.setAddress(userInfos.get(j).getLivingAddress());
					daili.setRealName(userInfos.get(j).getUserName());
					for(int m=0;m<areas.size();m++){
						if(areas.get(m).getAreaNo().equals(userInfos.get(j).getLivingAreaNo())){
							daili.setSheng(areas.get(m).getAreaFullName());
						}
					}
				}
			}
			for(int n=0;n<agentApplys.size();n++){
				if(agentApplys.get(n).getUserId().equals(list.get(i).getUserId())){
					daili.setYoushi(agentApplys.get(n).getApplyRemark());
					daili.setTime(StrUtil.intToTime(agentApplys.get(n).getLastModifyTime()));
					System.out.println("2017.08.15"+daili.getTime());
					daili.setAgentStatus(agentApplys.get(n).getAgentStatus());
					
				}
			}
			for(int k=0;k<accounts.size();k++){
				if(accounts.get(k).getUserId().equals(list.get(i).getUserId())){
					daili.setErjiNum(accounts.get(k).getDirectUsers());
					daili.setYijiNum(accounts.get(k).getIndirectUsers());
				}
			}
			dailis.add(daili);
		}
		pagerModel.setList(dailis);
		int count = lmUserDao.selectCount(userPager);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	/**
	 * @show 代理商申请通过
	 * 
	 * @param Integer
	 * 
	 * @return Integer
	 * */
	public int updateAgentCheckPass(Integer userId){
		
		 agentApplyDao.updateAgentCheckPass(userId);
		 lmUserDao.updateAgentCheckPass(userId);
		 return 1;
	}
	
	/**
	 * @show 代理商申请不通过
	 * 
	 * @param Integer
	 * 
	 * @return Integer
	 * */
    public int updateAgentCheckNoPass(Integer userId){
			
			 agentApplyDao.updateAgentCheckNoPass(userId);
			 lmUserDao.updateAgentCheckNoPass(userId);
			 return 2;
		}

	/*
	 * 获取拒绝原因
	 * */
	public List<JRefuseReason> selectAllRefuse(JRefuseReason refuse) {
		List<JRefuseReason> list=refuseDao.selectAll(refuse);
		return list;
	}

	/*
	 * 获取当前用户的审核权限  checkType 审核人员权限
	 * */
	public Integer selectCheckType(Integer userId) {
		SysUser sysUser=sysUserDao.selectByPrimaryKey(userId);
		System.out.println(sysUser+"******************");
		List<SysRolemenu> list=sysRoleDao.selectAllByRoleId(sysUser.getRoleId());
		Integer checkType=null;
		for(int i=0;i<list.size();i++){
			if(list.get(i).getMenuCode().equals("check_type_one")){
				checkType=1;
			}
			if(list.get(i).getMenuCode().equals("check_type_two")){
				checkType=2;
			}
			if(list.get(i).getMenuCode().equals("check_type_three")){
				checkType=3;
			}
		}
		System.out.println(checkType+"&&&&&&&");
		return checkType;
	}

	/*
	 * 如果是复审、终审，则要获取到初审记录的id
	 * */
	public JCheckArtificial selectMaxCheck(Integer userId) {
		JCheckArtificial check=new JCheckArtificial();
		check.setUserId(userId);
//		check.setType((byte)1);
		JCheckArtificial check1=checkDao.selectMax(check);
		return check1;
	}

	/*
	 * 审核人员添加审核记录
	 * */
	public void addCheck(JCheckArtificial check) {
		if(check.getType()!=(byte)3){
			check.setCredit(null);
		}
		if(check.getStatus()==(byte)1){
			check.setErrMsg(null);
		}/*else{
			check.setRemark(null);
		}*/
		System.out.println("审核记录"+check);
		checkDao.insert(check);//添加审核记录
		//初审，则添加初审id
		if(check.getType()==(byte)1){
			JCheckArtificial check1=new JCheckArtificial();
			check1.setCheckOneId(check.getRecId());
			check1.setRecId(check.getRecId());
			checkDao.updateByPrimaryKeySelective(check1);
		}
		LmSchedule schedule=new LmSchedule();
		schedule.setUserId(check.getUserId());
		schedule.setActionType((byte)1);
		schedule.setCreateTime(StrUtil.timeToInt(new Date()));
		schedule.setHandelFlag((byte)0);
		scheduleDao.insert(schedule);
		LmUserInfo lmUserInfo=lmUserInfoDao.selectByUserId(check.getUserId());
		LmUserInfo userInfo=new LmUserInfo();
		userInfo.setInfoId(lmUserInfo.getInfoId());
		if(check.getType()<(byte)3&&check.getStatus()==(byte)1){//初审、复审通过，状态为审核中
			userInfo.setAuthStep((byte)4);
		}
		if(check.getType()==1){//初审
			if(check.getStatus()==(byte)1){//初审通过
				userInfo.setAuthStep((byte)5); 
			}else if(check.getStatus()==(byte)2){//初审未通过
				userInfo.setAuthStep((byte)8);
				userInfo.setAuthStatus((byte)3);
			}else{
				userInfo.setAuthStep((byte)8); //初审材料补充
				userInfo.setAuthStatus((byte)3);
				userInfo.setCheckStatus((byte)1);
			}
		}
		if(check.getType()==2){//复审
			if(check.getStatus()==(byte)1){//复审通过
				userInfo.setAuthStep((byte)6);
			}else if(check.getStatus()==(byte)2){//复审未通过
				userInfo.setAuthStep((byte)9);
				userInfo.setAuthStatus((byte)3);
			}else{
				userInfo.setAuthStep((byte)9);//补充材料
				userInfo.setAuthStatus((byte)3);
				userInfo.setCheckStatus((byte)1);
			}
		}
		//终审
		if(check.getType()==3){
			userInfo.setLastUpdateTime(StrUtil.timeToInt(new Date()));
			if(check.getStatus()==1){
				userInfo.setAuthStep((byte)7);
				userInfo.setAuthStatus((byte)2);
				LmCredit lmCredit=creditDao.selectByUserId(check.getUserId());
				if(lmCredit==null){
					LmCredit credit1=new LmCredit();
					credit1.setUserId(check.getUserId());
					credit1.setTotalCredit(check.getCredit());
					credit1.setUsableCredit(check.getCredit());
					credit1.setDistributeCredit(check.getCredit());
					credit1.setPromoteCredit(new BigDecimal(0.00));
					credit1.setFrozenCredit(new BigDecimal(0.00));
					credit1.setTotalLoaned(new BigDecimal(0.00));
					creditDao.insert(credit1);
				}else{
					LmCredit credit=new LmCredit();
					credit.setCreditId(lmCredit.getCreditId());
					credit.setTotalCredit(check.getCredit());
					credit.setUsableCredit(check.getCredit());
					credit.setDistributeCredit(check.getCredit());
					creditDao.updateByPrimaryKeySelective(credit);
				}
				
			}else if(check.getStatus()==2){
				userInfo.setAuthStep((byte)10);
				userInfo.setAuthStatus((byte)3);
			}else{
				userInfo.setAuthStep((byte)10);//终审补充材料
				userInfo.setAuthStatus((byte)3);
				userInfo.setCheckStatus((byte)1);
			}
			
			/*if(check.getStatus()==2){
				//userInfo.setAuthStep((byte)6); 7.6
				userInfo.setAuthStatus((byte)3);
			}*/
		}
		System.out.println("用户详情&&&&&&&&&&&&&&&&&&&&&&&&&&"+userInfo);
		lmUserInfoDao.updateByPrimaryKeySelective(userInfo);
	}

	/*
	 * 判断用户状态，如果不是待审核，则不显示添加审核记录部分
	 * */
	public LmUserInfo selectUserInfoByUserId(Integer userId) {
		LmUserInfo userInfo=lmUserInfoDao.selectByUserId(userId);
		return userInfo;
	}

	//@Override
	public JCheckArtificial selectCheckById(Integer oneCheckId) {
		JCheckArtificial check=checkDao.selectByPrimaryKey(oneCheckId);
		return check;
	}

	
	/**
	 *@show  用户基本信息
	 *
	 *@param Integer
	 *
	 *@return LmUserInfo
	 * */
	public UBaseInfo selectBaseInfo(Integer userId) {
		UBaseInfo baseInfo=new UBaseInfo();
		LmUser user=lmUserDao.selectByPrimaryKey(userId);
		baseInfo.setUserId(userId);
		baseInfo.setMobile(user.getLoginMobile());
		baseInfo.setRegisterTime(StrUtil.intToTime(user.getCreateTime()));
		LmUserInfo recommend=lmUserInfoDao.selectByUserId(user.getRecommendId());
		if(recommend!=null){
			baseInfo.setRecommendName(recommend.getUserName());
		}
		
		LmUserInfo userInfo=lmUserInfoDao.selectByUserId(userId);
		if(userInfo!=null){
			baseInfo.setUserName(userInfo.getUserName());
			SysArea area=areaDao.selectByPrimaryKey(userInfo.getLivingAreaNo());
			if(area!=null){
				baseInfo.setAddressNow(area.getAreaFullName()+userInfo.getLivingAddress());
			}
			baseInfo.setCardOld(userInfo.getCardNo());
			baseInfo.setDesKey(Status.card_id);
			if(!StrUtil.isNull(userInfo.getCardNo())){
				baseInfo.setCardNum(DesUtils.decrypt(userInfo.getCardNo(), Status.card_id));
			}
			
			baseInfo.setLivingTimes(userInfo.getLivingTimes());
			baseInfo.setMaritalStatus(userInfo.getMaritalStatus());
		}
		
		GrabXuexin xuexin=xuexinDao.selectByUserId(userId);
		if(xuexin!=null){
			baseInfo.setSchoolName(xuexin.getSchoolName());
			baseInfo.setSpecialty(xuexin.getSpecialty());
		}
		
		//学历不知道去哪里查
		baseInfo.setThirdStudy(null);
		return baseInfo;
	}

	/**
	 *@show  用户工作信息获取
	 *
	 *@param Integer
	 *
	 *@return LmUserJob
	 * 
	 * */
	public LmUserJob selectWorkInfo(Integer userId) {
		LmUserJob workInfo=workInfoDao.selectByUserId(userId);
		System.out.println("%%%%%%%%%%%%%%%%%%%%"+workInfo);
		if(workInfo!=null){
			SysArea area=areaDao.selectByPrimaryKey(workInfo.getCompanyAreaNo());
			System.out.println("**************"+area);
			if(area!=null){
				if(StrUtil.isNull(workInfo.getCompanyAreaNo())){
					workInfo.setAddress(area.getAreaFullName()+workInfo.getCompanyAddress());
				}
			}
				
		}
		return workInfo;
	}

	/**
	 *@show  获取紧急联系人
	 *
	 *@param Integer
	 *
	 *@return LmUserEmergency
	 * 
	 * */
	public LmUserEmergency selectEmergency(Integer userId) {
		LmUserEmergency userEmergency=userEmergencyDao.selectByUserId(userId);
		return userEmergency;
	}

	/**
	 *@show 用户银行卡信息
	 *
	 *@param Integer
	 *
	 *@return LmUserBank
	 * 
	 * */
	public LmUserBank selectUserBank(Integer userId) {
		LmUserBank userBank=userBankDao.selectByUserId(userId);
		if(userBank!=null){
			userBank.setBankAccount(DesUtils.decrypt(userBank.getBankAccount(), Status.bank_account));
		}
		return userBank;
	}

	/**
	 *@show 查询用户芝麻信用分
	 *
	 *@param Integer
	 *
	 *@return GrabZhima
	 * */
	public GrabZhima selectZhiMa(Integer userId) {
		GrabZhima zhima=zhimaDao.selectByUserId(userId);
		return zhima;
	}

    /**
     *@show 获取运营商信息
     *
     *@param Integer
     *
     *@return GrabCarrierUserinfo
     * */
	public GrabCarrierUserinfo selectCrarrier(Integer userId) {
		GrabCarrierUserinfo carrier=carrierDao.selectByUserId(userId);
		if(carrier !=null){
			if(!StrUtil.isNull(carrier.getIdcard())){
				carrier.setIdcard(DesUtils.decrypt(carrier.getIdcard(), Status.card_id));
			}
			carrier.setLastModifyTimeD(StrUtil.intToTime(carrier.getLastModifyTime()));
			carrier.setOpenTimeD(StrUtil.intToTime(carrier.getOpenTime()));
			if(carrier.getAvailableBalance()!=null){
			if(carrier.getAvailableBalance().equals("null")||carrier.getAvailableBalance().equals("")||carrier.getAvailableBalance()==null){
				carrier.setAvailableBalance("0");
				carrier.setAvailableBalancef(Double.parseDouble(carrier.getAvailableBalance())/100);
				
			}else{
				carrier.setAvailableBalancef(Double.parseDouble(carrier.getAvailableBalance())/100);
			}
			}
			
		}
		
		return carrier;
	}

	/**
	 *@show 亲情网抓取信息
	 *
	 *@param Integer
	 *
	 *@return List<GrabCarrierFamilies>list
	 * */
	public List<GrabCarrierFamilies> selectCarrierFamilies(HttpServletRequest request,Integer userId){
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=carrierFamiliesDao.familyCount(userId);
		List<GrabCarrierFamilies> carrierFamilies=new ArrayList<GrabCarrierFamilies>();
		   if(pageNow!=null){
				pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
		        carrierFamilies =carrierFamiliesDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
		        for(int i=0;i<carrierFamilies.size();i++){
					carrierFamilies.get(i).setJoinDateD(StrUtil.intToTime(carrierFamilies.get(i).getJoinDate()));
				}
				}else{
				pageMd=new PageModel(totalCount,1);
				carrierFamilies =carrierFamiliesDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
				for(int i=0;i<carrierFamilies.size();i++){
					carrierFamilies.get(i).setJoinDateD(StrUtil.intToTime(carrierFamilies.get(i).getJoinDate()));
				}
		  } 
		 
		 
		  request.setAttribute("CURRENT1",pageMd.getPageNow());
		  request.setAttribute("totalPageCount1",pageMd.getTotalPageCount());
		return carrierFamilies;
	}

	/**
	 *@show 账单信息抓取
	 *
	 *@param Integer
	 *
	 *@return List<GrabCarrierBills>list
	 * */
	public List<GrabCarrierBills> selectBills(HttpServletRequest request,Integer userId){
		String pageNow = request.getParameter("pageNow"); //获取当前页
		System.out.println(pageNow);
		PageModel pageMd=null;
		Integer totalCount=carrierBillsDao.billsCount(userId);
		List<GrabCarrierBills> carrierBills=new ArrayList<GrabCarrierBills>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			carrierBills=carrierBillsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<carrierBills.size();i++){
				carrierBills.get(i).setActualfeef(Double.parseDouble(carrierBills.get(i).getActualfee())/100);
				carrierBills.get(i).setTotalFeef(Double.parseDouble(carrierBills.get(i).getTotalFee())/100);
			}
	        
			}else{
			pageMd=new PageModel(totalCount,1);
			carrierBills=carrierBillsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<carrierBills.size();i++){
				carrierBills.get(i).setActualfeef(Double.parseDouble(carrierBills.get(i).getActualfee())/100);
				carrierBills.get(i).setTotalFeef(Double.parseDouble(carrierBills.get(i).getTotalFee())/100);
			}
	  } 
		 request.setAttribute("CURRENT2",pageMd.getPageNow());
		  request.setAttribute("totalPageCount2",pageMd.getTotalPageCount());
		return carrierBills;
	}

	
	/**
	 *@show 通话明细
	 *
	 *@param Integer
	 *
	 *@return List<GrabCarrierCalls>list
	 * */
	public List<GrabCarrierCalls> selectCalls(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=carrierCallsDao.callsCount(userId);
		List<GrabCarrierCalls> carrierCalls=new ArrayList<GrabCarrierCalls>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			carrierCalls=carrierCallsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<carrierCalls.size();i++){
				carrierCalls.get(i).setCallTimeD(StrUtil.intToTime(carrierCalls.get(i).getCallTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			carrierCalls=carrierCallsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<carrierCalls.size();i++){
				carrierCalls.get(i).setCallTimeD(StrUtil.intToTime(carrierCalls.get(i).getCallTime()));
			}
	  } 
		
		 request.setAttribute("CURRENT3",pageMd.getPageNow());
		 request.setAttribute("totalPageCount3",pageMd.getTotalPageCount());
		return carrierCalls;
	}

	/**
	 *@show 短信详情明细抓取
	 *
	 *@param Integer
	 *
	 *@return List<GrabCarrierSmses>list
	 * */
	public List<GrabCarrierSmses> selectSmses(HttpServletRequest request,Integer userId){
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=carrierSmsesDao.smsesCount(userId);
		List<GrabCarrierSmses> smses=new ArrayList<GrabCarrierSmses>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			smses=carrierSmsesDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<smses.size();i++){
				smses.get(i).setSmsTimeD(StrUtil.intToTime(smses.get(i).getSmsTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			smses=carrierSmsesDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<smses.size();i++){
				smses.get(i).setSmsTimeD(StrUtil.intToTime(smses.get(i).getSmsTime()));
			}
	    } 
		request.setAttribute("CURRENT4",pageMd.getPageNow());
		request.setAttribute("totalPageCount4",pageMd.getTotalPageCount());	
		return smses;
	}

	/**
	 *@show 流量信息抓取
	 *
	 *@param Integer
	 *
	 *@param List<GrabCarrierNets>list
	 * */
	public List<GrabCarrierNets> selectNets(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=carrierNetsDao.netsCount(userId);
		List<GrabCarrierNets> nets=new ArrayList<GrabCarrierNets>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			nets=carrierNetsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<nets.size();i++){
				nets.get(i).setNetsTimeD(StrUtil.intToTime(nets.get(i).getNetsTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			nets=carrierNetsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<nets.size();i++){
				nets.get(i).setNetsTimeD(StrUtil.intToTime(nets.get(i).getNetsTime()));
			}
	    } 
		request.setAttribute("CURRENT5",pageMd.getPageNow());
		request.setAttribute("totalPageCount5",pageMd.getTotalPageCount());
		return nets;
	}

	/**
	 *@show qq用户详细信息
	 *
	 *@param Integer
	 *
	 *@return List<GrabQqUserinfo>
	 * */
	public List<GrabQqUserinfo> selectQQuserINfo(Integer userId) {
		List<GrabQqUserinfo> qqUserInfos=qqUserinfoDao.selectByUserId(userId);
		return qqUserInfos;
	}
   
	/**
	 *@show qq好友信息抓取 xiugai 7.1 qq
	 * 
	 *@param  Integer
	 *
	 *@return List<GrabQqContacts>list
	 * */
	public List<GrabQqContacts> selectQQContacts(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=qqContactsDao.qqContactsCount(userId);
		List<GrabQqContacts> qqContacts=new ArrayList<GrabQqContacts>();
		List<GrabQqRecentcontacts> recent=new ArrayList<GrabQqRecentcontacts>();	
		
       if(pageNow!=null){
			
	        pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			qqContacts=qqContactsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			recent=recentDao.selectByUserId(userId);
			for(int i=0;i<qqContacts.size();i++){
				for(int j=0;j<recent.size();j++){
					if(qqContacts.get(i).getRelationqqnum().equals(recent.get(j).getContactnum())){
						qqContacts.get(i).setLastTime(recent.get(j).getContacttime());
						break;
					}
				}
			}
		}else{
			pageMd=new PageModel(totalCount,1);
			qqContacts=qqContactsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			recent=recentDao.selectByUserId(userId);
			for(int i=0;i<qqContacts.size();i++){
				for(int j=0;j<recent.size();j++){
					if(qqContacts.get(i).getRelationqqnum().equals(recent.get(j).getContactnum())){
						qqContacts.get(i).setLastTime(recent.get(j).getContacttime());
						break;
					}
				}
			}
	  } 
				
	  
	   int a=pageMd.getPageNow();
      request.setAttribute("CURRENT",a);
      request.setAttribute("totalPageCount",pageMd.getTotalPageCount());
		return qqContacts;
	}

	
	
	/**
	 *@show 淘宝采集字段
	 *
	 *@param Integer
	 *
	 *@return GrabTaobaoUserinfo
	 * */
	public GrabTaobaoUserinfo selectTaobao(Integer userId) {
		GrabTaobaoUserinfo taobaoBase=taobaoBaseDao.selectByUserId(userId);
		return taobaoBase;
	}

	/**
	 *@show  支付宝资产
	 * 
	 * @param Integer
	 * 
	 * @return List<GrabTaobaoAlipaywealth>list
	 * 
	 * */
	public List<GrabTaobaoAlipaywealth> selectAlipaywealth(Integer userId) {
		List<GrabTaobaoAlipaywealth> list=alipayweathDao.selectByUserId(userId);
		return list;
	}

	/**
	 *@show  淘宝收货地址
	 *
	 *@param Integer
	 *
	 *@return List<GrabTaobaoDeliveraddress>list
	 * */
	public List<GrabTaobaoDeliveraddress> selectTaobaoAddress(Integer userId) {
		List<GrabTaobaoDeliveraddress> list=taobaoAddressDao.selectByUserId(userId);
		return list;
	}

	/**
	 *@show 最近订单的收货地址
	 *
	 *@param Integer
	 *
	 *@return List<GrabTaobaoRecentdeliveraddress>
	 * */
	public List<GrabTaobaoRecentdeliveraddress> selectRecentAddress(
			Integer userId) {
		List<GrabTaobaoRecentdeliveraddress> list=recentAddressDao.selectByUserId(userId);
		//xiugai 6.30
		List<String> tradIdlist= new ArrayList<String>();
		for(GrabTaobaoRecentdeliveraddress address:list){
			tradIdlist.add(address.getTradeId());
		    }
		List<GrabTaobaoTradedetails> selectAll=new ArrayList <GrabTaobaoTradedetails>();
		for(String in:tradIdlist){
			GrabTaobaoTradedetails info1 = TradedetailDao.selectShopName(in);
			
			if(info1!=null){
			selectAll.add(info1);
			}
		}
			for(GrabTaobaoRecentdeliveraddress user:list){
				for(GrabTaobaoTradedetails info:selectAll){
					
					if(user.getTradeId().equals(info.getTradeId())){
						user.setSellerShopname(info.getSellerShopname());
						break;
						
					}
					
				}
				
			}
		return list;
	}

	/**
	 *@show  学信网采集字段
	 *
	 *@param Integer
	 *
	 *@return GrabXuexin
	 * 
	 * */
	public GrabXuexin selectXueXin(Integer userId) {
		GrabXuexin xuexin=xuexinDao.selectByUserId(userId);
		return xuexin;
	}

	/**
	 *@show 借款信息
	 *
	 *@param Integer
	 *
	 *@return UUserLoan
	 * */
	public UUserLoan selectUserLoan(Integer userId) {
		UUserLoan userLoan=new UUserLoan();
		LmUser user=lmUserDao.selectByPrimaryKey(userId);
//		userLoan.setAgentStatus(user.getAgentStatus());
		//用户状态默认正常
//		userLoan.setStatus(user.getLoginLock());
		LmCredit credit=creditDao.selectByUserId(userId);
		if(credit !=null){
			userLoan.setCredit(credit.getTotalCredit());
			userLoan.setMoneyUse(credit.getUsableCredit());
		}
		
		LmAccount account=accountDao.selectByUserId(userId);
		if(account !=null){
			userLoan.setMoney(account.getAccountBalance());
			userLoan.setPrizeMoney(account.getPrizeMoney());
		}
		
		return userLoan;
	}

	/**
	 *@show  提现列表
	 *
	 *@param Integer
	 *
	 *@return List<ULoan>list
	 * */
	public List<ULoan> selectLoan(HttpServletRequest request,Integer userId) {
		List<LmWithdraw> withDraws=withdrawDao.selectByUserId(userId);
		List<LmUserBank> banks=userBankDao.selectAllUser();
		List<ULoan> loans=new ArrayList<ULoan>();
		for(int i=0;i<withDraws.size();i++){
			ULoan loan=new ULoan();
			for(int j=0;j<banks.size();j++){
				if(withDraws.get(i).getBankId()==banks.get(j).getBankId()){
					loan.setBankNumber(banks.get(j).getBankAccount());
				}
			}
			
			loan.setCreateTime(StrUtil.intToTime(withDraws.get(i).getCreateTime()));
			loan.setMoney(withDraws.get(i).getWithdrawMoney());
			loan.setStatus(withDraws.get(i).getPayStatus());
			loan.setCheckStatus(withDraws.get(i).getCheckStstus());
			loans.add(loan);
		}
		return loans;
	}

	/**
	 *@show 微信信息
	 *
	 *@param Integer
	 *
	 *@return GrabWeixin
	 * */
	public GrabWeixin selectWeixin(Integer userId) {
		GrabWeixin weixin=grabWeixinDao.selectByUserId(userId);
		return weixin;
	}

	/**
	*@show 账单信息   最近的还款记录
	*
	*@param Integer
	*
	*@return LmRepay
    *
	*/
	public LmRepay selectMaxRepay(Integer userId) {
		//查找还款中或待还款的记录
		LmRepay repay=repayDao.selectMaxByUserId(userId);
		if(repay !=null){
			LmLoan loan=loanDao.selectByPrimaryKey(repay.getLoanNo());
			repay.setLoanTime(StrUtil.intToTime(loan.getLoanTime()));
			repay.setRepayEndTimeD(StrUtil.intToTime(repay.getRepayEndTime()));
			repay.setLoanMoney(loan.getLoanMoney());
			repay.setFenqi(loan.getLoanMoney().divide(new BigDecimal(repay.getTotalStage()), 2, RoundingMode.HALF_UP)+"元*"+loan.getLoanDays()/30/repay.getTotalStage()+"个月");
			LmUserBank userBank=userBankDao.selectByUserId(userId);
			if(userBank!=null){
				repay.setBankName(userBank.getBankName());
				repay.setBankNumber(DesUtils.decrypt(userBank.getBankAccount(), Status.card_id));
			}
			String zhangdan="";
			if(repay.getAutoFlag()==(byte)0){
				zhangdan="自动打款";
			}else{
				zhangdan="手工打款";
			}
			if(repay.getRepayStatus()==(byte)0){
				zhangdan+="待还款";
			}else if(repay.getRepayStatus()==(byte)1){
				zhangdan+="还款中";
			}else if(repay.getRepayStatus()==(byte)2){
				zhangdan+="还款成功";
			}else if(repay.getRepayStatus()==(byte)3){
				zhangdan+="提前还款";
			}
			repay.setZhangdan(zhangdan);
			List<LmRepay> selectByuserIdStatus = repayDao.selectByuserIdStatus(userId);
			repay.setHuankuan(selectByuserIdStatus.size()+"笔未还清");
			
		}
		return repay;
	}

	/**
	 *@show 还款记录
	 *
	 *@param Integer
	 *
	 *@return List<LmRepay>
	 * */
	public List<LmRepay> selectReplay(Integer userId) {
		List<LmRepay> list=repayDao.selectByUserId(userId);
		List<JCollection> collections=collecionDao.selectByUserId(userId);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(int i=0;i<list.size();i++){
			list.get(i).setRepayEndTimeD(StrUtil.intToTime(list.get(i).getRepayEndTime()));
			list.get(i).setRepayYesTimeD(StrUtil.intToTime(list.get(i).getRepayYesTime()));
			if(list.get(i)!=null){
			LmLoan loan=loanDao.selectByPrimaryKey(list.get(i).getLoanNo());
			list.get(i).setLoanTime(StrUtil.intToTime(loan.getLoanTime()));
			list.get(i).setLoandays(loan.getLoanDays());
			}
			for(int j=0;j<collections.size();j++){
				if(list.get(i).getRepayNo().equals(collections.get(j).getRepayNo())){
					list.get(i).setRemark(collections.get(j).getRemark());
				}
			}
		}
		return list;
	}

	/**
	 *@show 附件信息
	 *
	 *@param Integer
	 *
	 *@return GrabFaceRecognition
	 * */
	public GrabFaceRecognition selectFaceRecognition(Integer userId) {
		GrabFaceRecognition face=faceDao.selectByUserId(userId);
		return face;
	}

	/**
	 *@show  短信记录
	 *
	 *@param Integer
	 * 
	 *@return List<LogSms>list
	 * */
	public List<LogSms> selectLogSms(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=logSmsDao.smsjlCount(userId);
		List<LogSms> smses=new ArrayList<LogSms>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			smses=logSmsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<smses.size();i++){
				smses.get(i).setSendTimeD(StrUtil.intToTime(smses.get(i).getSendTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			smses=logSmsDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<smses.size();i++){
				smses.get(i).setSendTimeD(StrUtil.intToTime(smses.get(i).getSendTime()));
			}
	    } 
		request.setAttribute("CURRENT7",pageMd.getPageNow());
		request.setAttribute("totalPageCount7",pageMd.getTotalPageCount());
		
		return smses;
	}

	/**
	 *@show 微信记录
	 *
	 *@param Integer
	 *
	 *@return List<LogWeixin>list
	 * */
	public List<LogWeixin> selectLogWeixin(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=logWeixinDao.weixinCount(userId);
		List<LogWeixin>list=new ArrayList<LogWeixin>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			list=logWeixinDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<list.size();i++){
				list.get(i).setSendTimeD(StrUtil.intToTime(list.get(i).getSendTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			list=logWeixinDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			for(int i=0;i<list.size();i++){
				list.get(i).setSendTimeD(StrUtil.intToTime(list.get(i).getSendTime()));
			}
	    } 
		request.setAttribute("CURRENT8",pageMd.getPageNow());
		request.setAttribute("totalPageCount8",pageMd.getTotalPageCount());
		
		return list;
	}

	/**
	 *@show 查询用户手机情况
	 *
	 *@param Integer
	 * 
	 *@return List<LmUserLoginLog>list
	 * */
	public List<LmUserLoginLog> selectLoginLog(HttpServletRequest request,Integer userId) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=userLoginDao.loginCount(userId);
		List<LmUserLoginLog> list=new ArrayList<LmUserLoginLog>();
		List<SysArea> areas=new ArrayList<SysArea>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			list=userLoginDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			areas=areaDao.selectAllUser();
			for(int i=0;i<list.size();i++){
				list.get(i).setLoginTimeD(StrUtil.intToTime(list.get(i).getLoginTime()));
				for(int j=0;j<areas.size();j++){
					if(areas.get(j).getAreaNo().equals(list.get(i).getLoginAreaCode())){
						list.get(i).setAddress(areas.get(j).getAreaFullName());
					}
				}
			}
			
			}else{
			pageMd=new PageModel(totalCount,1);
			list=userLoginDao.selectByUserId(pageMd.getStartPos(),pageMd.getPageSize(),userId);
			areas=areaDao.selectAllUser();
			for(int i=0;i<list.size();i++){
				list.get(i).setLoginTimeD(StrUtil.intToTime(list.get(i).getLoginTime()));
				for(int j=0;j<areas.size();j++){
					if(areas.get(j).getAreaNo().equals(list.get(i).getLoginAreaCode())){
						list.get(i).setAddress(areas.get(j).getAreaFullName());
					}
				}
			}
			}
	    
		request.setAttribute("CURRENT9",pageMd.getPageNow());
		request.setAttribute("totalPageCount9",pageMd.getTotalPageCount());
		
		return list;
	}

	/**
	 *@show 机审得分
	 *
	 *@param Integer
	 *
	 *@return LmUserCheckLog
	 * */
	public LmUserCheckLog selectMaxCheckLog(Integer userId) {
		LmUserCheckLog checkLog=checkLogDao.selectMaxByUserId(userId);
		return checkLog;
	}
	
    /**
    *@show 审核记录审核状态   审核记录
    *
    *@param JCheckArtificial
    *
    *@return List<JCheckArtificial>
	* 
	*/
	public List<JCheckArtificial> selectCheckArtifs(JCheckArtificial artificial) {
		List<JCheckArtificial> list=checkDao.selectByArtifs(artificial);
		JRefuseReason r=new JRefuseReason();
		List<JRefuseReason> refuses=refuseDao.selectAll(r);
		List<SysUser> sysUsers=sysUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			if(list.get(i).getStatus()==(byte)2){
				for(int j=0;j<refuses.size();j++){
					if(refuses.get(j).getRecId()==list.get(i).getErrMsg()){
						list.get(i).setRemark(refuses.get(j).getBack());
					}
				}
			}
			for(int k=0;k<sysUsers.size();k++){
				if(list.get(i).getAdminId()==sysUsers.get(k).getUserId()){
					list.get(i).setAdminName(sysUsers.get(k).getLoginName());
				}
			}
		}
		return list;
	}

	/**
	 *@show  推广信息
	 *
	 *@param  Integer
	 *
	 *@return LmUser
	 * */
	public LmUser selectByUserId(Integer userId) {
		LmUser user=lmUserDao.selectByPrimaryKey(userId);
		return user;
	}

	/**
	 *@show 推广关系列表
	 *
	 *@param Integer
	 *
	 *@return List<TuiGuang>
	 * */
	public List<TuiGuang> selectTuiGuang(Integer userId) {
		List<TuiGuang> tuiguangs=new ArrayList<TuiGuang>();
		Integer[] userIds={userId};
		int m=0;
		List<TuiGuang> lmUsers=selectGuanXi(userIds,m);
		while(lmUsers!=null&&lmUsers.size()>0){
			userIds=new Integer[lmUsers.size()];
			for(int i=0;i<lmUsers.size();i++){
				userIds[i]=lmUsers.get(i).getUserId();
			}
			tuiguangs.addAll(lmUsers);
			m++;
			lmUsers=selectGuanXi(userIds,m);
		}
		return tuiguangs;
	}

	private List<TuiGuang> selectGuanXi(Integer[] userIds,Integer m) {
		List<LmUser> lmUsers=lmUserDao.selectTuiGuang(userIds);
		List<TuiGuang> tuiguangs=new ArrayList<TuiGuang>();
		for(int i=0;i<lmUsers.size();i++){
			TuiGuang tuiguang=new TuiGuang();
			if(m==0){
				tuiguang.setIsDirect((byte)1);
			}else{
				tuiguang.setIsDirect((byte)2);
			}
			if(lmUsers.get(i).getTotalCreditLoan().doubleValue()>0.00001){
				tuiguang.setLoanMoney("累计借款"+lmUsers.get(i).getTotalCreditLoan());
			}else{
				tuiguang.setLoanMoney("未借款");
			}
			
			tuiguang.setRegistTime(StrUtil.intToTime(lmUsers.get(i).getCreateTime()));
			tuiguang.setLoginMobile(lmUsers.get(i).getLoginMobile());
			tuiguang.setUserId(lmUsers.get(i).getUserId());
			tuiguangs.add(tuiguang);
		}
		return tuiguangs;
	}

	/**
	 *@show 个人奖励
	 *
	 *@param LmAccountLog
	 *
	 *@return List<LmAccountLog>list
	 * */
	public List<LmAccountLog> selectAccountLog(HttpServletRequest request,LmAccountLog account) {
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=accountLogDao.accountCount(account);
		System.out.println(totalCount);
		List<LmAccountLog> accounts=new ArrayList<LmAccountLog>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			account.setOffset(pageMd.getStartPos());
			account.setPageSize(pageMd.getPageSize());
			accounts=accountLogDao.selectByAccount(account);
			for(int i=0;i<accounts.size();i++){
				accounts.get(i).setCreateTimeD(StrUtil.intToTime(accounts.get(i).getCreateTime()));
			}
			}else{
			pageMd=new PageModel(totalCount,1);
			account.setOffset(pageMd.getStartPos());
			account.setPageSize(pageMd.getPageSize());
			accounts=accountLogDao.selectByAccount(account);
			for(int i=0;i<accounts.size();i++){
				accounts.get(i).setCreateTimeD(StrUtil.intToTime(accounts.get(i).getCreateTime()));
			}
	    } 
		request.setAttribute("CURRENT10",pageMd.getPageNow());
		request.setAttribute("totalPageCount10",pageMd.getTotalPageCount());
	
		return accounts;
	}

	/**
	 *@show 催收记录
	 *
	 *@param Integer
	 *
	 *@return List<JCollection>list
	 * */
	public List<JCollection> selectCuiShou(Integer userId) {
		List<JCollection> cuishous=collecionDao.selectByUserId(userId);
		return cuishous;
	}

	/**
	 *@show 添加催收
	 *
	 *@param JCollection
	 *
	 *@return 
	 * */	
	public void addCuiShou(JCollection cuishou) {
		LmRepay repay=repayDao.selectByPrimaryKey(cuishou.getRepayNo());
		cuishou.setLoanAmount(repay.getLoanCapital());
		LmLoan loan=loanDao.selectByPrimaryKey(repay.getLoanNo());
		cuishou.setLoanTime(loan.getLoanTime());
		cuishou.setOverdueDays(repay.getOverdueDays());
		cuishou.setShouldAmount(repay.getLoanCapital().add(repay.getLoanInterest()));
		cuishou.setUserId(repay.getUserId());
		LmUser user=lmUserDao.selectByPrimaryKey(repay.getUserId());
		cuishou.setUserMobile(user.getLoginMobile());
		cuishou.setUserName(user.getNickName());
		collecionDao.insert(cuishou);
	}

	/**
	 *@show 获取最新的一条申请记录
	 *
	 *@param Integer
	 *
	 *@return LmAgentApply
	 * */
	public LmAgentApply selectAgentApply(Integer userId) {
		LmAgentApply agentApply=agentApplyDao.selectMaxByUserId(userId);
		if(agentApply!=null){
			SysArea area=areaDao.selectByPrimaryKey(agentApply.getAgentAreaCode());
			if(area!=null){
				agentApply.setAddress(area.getAreaFullName());
			}
		}
		
		return agentApply;
	}

	//@Override
	public void updateUser(LmUser user) {
		lmUserDao.updateByPrimaryKeySelective(user);
		
	}

	//@Override
	public void updateAgentApply(LmAgentApply agentApply) {
		agentApplyDao.updateByPrimaryKeySelective(agentApply);
		
	}

	//@Override
	public List<LmUserInfo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	//@Override
	public LmUserInfo selectAll1(Integer userId) {
		// TODO Auto-generated method stub
		return null;
	}
    /**
     *@show 获取qq好友总数
     *
     *@param Integer
     *
     *@return Integer
     *
     * 
     * */
	public Integer qqContactsCount(Integer userId){
		Integer qqContactsCount = qqContactsDao.qqContactsCount(userId);
		return qqContactsCount;
	}
	
	/**
	 *@show 手机通讯录
	 *
	 *@param Integer
	 * 
	 *@return List<GrabMobileContacts>list
	 * */
	public List<GrabMobileContacts>selectMobileByUserId(HttpServletRequest request,Integer userId){
		System.out.println(userId);
		String pageNow = request.getParameter("pageNow"); //获取当前页
		PageModel pageMd=null;
		Integer totalCount=grabMobileContactsMapper.mobileContactsCount(userId);
		System.out.println("总条数"+totalCount);
		List<GrabMobileContacts>list=new ArrayList<GrabMobileContacts>();
		if(pageNow!=null){
			pageMd=new PageModel(totalCount,Integer.parseInt(pageNow));
			list=grabMobileContactsMapper.selectByUserId(pageMd.getStartPos(), pageMd.getPageSize(), userId);
			}else{
			pageMd=new PageModel(totalCount,1);
			list=grabMobileContactsMapper.selectByUserId(pageMd.getStartPos(), pageMd.getPageSize(), userId);
	    } 
		System.out.println(list);
		request.setAttribute("MobileCUR",pageMd.getPageNow());
		request.setAttribute("MobileTOTAL",pageMd.getTotalPageCount());
		
		
		return list;
	}
	
	//补充材料用户
	public LmUserPager selectCailiaobucUserPager(LmUserPager pagerModel) {
		List<LmUser> list = lmUserDao.selectCailiaobucUser(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setLastLoginTimeD(StrUtil.intToTime(list.get(i).getLastLoginTime()));
		}
		pagerModel.setList(list);
		
		int count = lmUserDao.selectCailiaobucCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
		
	}

	
	
	

}
